<?php
    mysql_query("SET NAMES 'utf8'");
    
    /*
    *	the execSQL function is used for execing a sql statement
    *	@param $sql: sql statement
    *	result: a resourse
    */
	function execSQL($sql){
		$rs = @mysql_query($sql);
		if(isset($rs)){
			return $rs;
		}
	}
    
    /*
    *   Ham phan trang
    *   $page: trang hien tai
    *   $row_total: tong so record 
    *   $pagesize: so record muon hien thi tren mot trang
    *   $pagegroup: so index cua trang muon hien thi
    *   $url la url se request khi click vao index page
    */   
    function pagenavigator($page, $row_total, $page_size, $pagegroup_size, $url){
        $andpage    = (strrpos ($url,"?")===false)?"?page=":"&page=";
        $page_total = floor(($row_total - 1) / $page_size) + 1;
        if(!$page || $page > $page_total || $page < 1){
            $page = 1;
        }
        
        $group      = floor(($page-1) / $pagegroup_size) + 1;
        $start_page = (($group-1) * $pagegroup_size) + 1;
        $end_page   = $start_page + $pagegroup_size - 1;
        if($end_page > $page_total){
            $end_page   = $page_total;
        }
        if($page_total > 1){
            $str    = '';
            if($end_page > $pagegroup_size){
                $start_group    = $pagegroup_size;
            }
            else{
                $start_group    = 0;
            }
            if($page > 2){
                $str .= '<a href="' . $url . '&p=1" > First page </a>';
            }
            if($group > 1){
                $str .= '<a href="' . $url . '&p=' . ($page - 1) . '" > << </a>';
            }
            if($page!=1&&$page!=$page_total){
                $str .= '<a href="' . $url . '&p=' . ($page - 1) . '" > << </a>';
            }
            for($i = $start_page; $i <= $end_page; $i++){
                $j      = $i + $page_size;
                $begin  = ($i - 1) * $page_size + 1;
                $end    = $begin + $page_size - 1;
                if($i == $page){
                    $str .= "<a class='current'>$i</a>";
                }
                else{
                    $str .= '<a href="' . $url . '&p=' . $i . '" > ' . $i . '</a>';
                }
            }
            if($page_total - $end_page > $pagegroup_size){
                $end_group  = $pagegroup_size;
            }
            else{
                $end_group  = $page_total - $end_page;
            }
            if($page < $page_total){
                $str .= '<a href="' . $url . '&p=' . ($page + 1) . '" > >> </a>';
            }
            if($page_total > 2 && $page < $page_total){
                $str .= '<a href="' . $url . '&p=' . $page_total . '" > Last page </a>';
            }
            return $str;
        }
    }
    
    /*
    *	the pagging function is used for pagging data retrieved from database
    *	@param $totalRecords: total records are retrieved from database
    *	@pram $limit: limit numberic record
    *	$param $css: css for links
    *   @param $urll : url for links
    */
	function pagging($totalRecords,$offset, $limit, $url = null){	
		$totalPages = ceil($totalRecords/$limit);
		if($totalPages>1){
			$next = $offset + $limit;
			$prev = $offset - $limit;
			$pagers = "";
			$current = ($offset/$limit) + 1;
			if($current != 1){
				$pagers .="<a href='".$url."&p=".$prev."'>"."Previous"."</a>";
			}
			for($p =1; $p<=$totalPages; $p++){
				if($current != $p){
					$pagers .= "<a href = '".$url."&p=".$limit*($p-1)."'>".$p."</a>";
				}else{
					$pagers .= "<a class = 'current'>".$p."</a>";
				}
			}
			if($current != $totalPages){
				$pagers .= "<a href = '".$url."&p=".$next."'>"."Next"."</a>";
			}
		}
		return $pagers;
	}
    
    /*
    *	totalRecords function is used to get the number of records from database
    *	@param $sql: sql statement
    *	result total record
    */
    function totalRecords($sql){
    	$rs = execSQL($sql);
    	if(isset($rs)){
    		$totalRecords = mysql_num_rows($rs);
    		return $totalRecords;
    	}else{
    		return 0;
    	}
    }
    
    /*
    *   Ham cat so ky tu trong chuoi
    *   $str:   chuoi ky tu can cat
    *   $len:   so ky tu giu lai
    *   $more:  vi tri bat dau cat
    */
    
    function cut_string($str,$len,$more){
        if ($str=='' || $str==NULL) return $str;
        if (is_array($str)) return $str;
        $str = trim($str);
        if (strlen($str) <= $len) return $str;
        $str = substr($str,0,$len);
        if ($str != '') {
            if (!substr_count($str," ")) {
            if ($more) $str .= " ...";
            return $str;
            }
            while(strlen($str) && ($str[strlen($str)-1] != " ")) $str = substr($str,0,-1);
            $str = substr($str,0,-1);
            if ($more) $str .= " ...";
        }
        return $str;
    } 
?>